// 引入需要的api
import { defineStore } from "pinia";
// 引入接口函数
import { reqGetCategory1ListData, reqGetCategory2ListData, reqGetCategory3ListData } from '@/api/product/category'
import type { CategoryStateModel } from "@/api/product/model/categoryModel";
// 创建模块对象
export const useCategoryListStore = defineStore('categoryList', {
    state: (): CategoryStateModel => {
        return {
            // 以下属性用来存储用户选择的id
            category1Id: undefined,
            category2Id: undefined,
            category3Id: undefined,
            // 以下属性用来存储用户选择的具体内容数据
            category1List: [],
            category2List: [],
            category3List: []
        }
    },
    actions: {
        // 1.获取一级分类数据
        async getCategory1ListData() {
            const result = await reqGetCategory1ListData()
            this.category1List = result
        },
        // 2. 获取二级分类数据
        async getCategory2ListData(id: number) {
            // 一旦一级导航变化,二级和三级导航需要重置
            this.category2Id = undefined
            this.category3Id = undefined
            this.category3List = [] // 将三级列表也要清空

            this.category1Id = id
            this.category2List = await reqGetCategory2ListData(id)
        },
        // 3. 获取三级分类数据
        async getCategory3ListData(id: number) {
            // 同理，当我修改了二级分类的时候，三级分类也需要重置
            // 重置三级id
            this.category3Id = undefined


            this.category2Id = id
            this.category3List = await reqGetCategory3ListData(id)
        },
        // 4. 设置三级分类ID
        setCategory3Id(id: number) {
            this.category3Id = id
        }
    },
    getters: {}
})